import io.github.lnyocly.ai4j.config.OpenAiConfig;
import io.github.lnyocly.ai4j.platform.openai.chat.entity.ChatCompletion;
import io.github.lnyocly.ai4j.platform.openai.chat.entity.ChatCompletionResponse;
import io.github.lnyocly.ai4j.platform.openai.chat.entity.ChatMessage;
import io.github.lnyocly.ai4j.service.Configuration;
import io.github.lnyocly.ai4j.service.IChatService;
import io.github.lnyocly.ai4j.service.PlatformType;
import io.github.lnyocly.ai4j.service.factor.AiService;
import okhttp3.OkHttpClient;

/**
 * kimi 目前不能用，目前用 openai
 */
public class KimiChat {

    private final OkHttpClient okHttpClient;
    private final String system;

    public KimiChat(OkHttpClient okHttpClient, String system) {
        this.okHttpClient = okHttpClient;
        this.system = system;
    }

    public ChatCompletionResponse chat(String content) throws Exception {
        Configuration configuration = new Configuration();
        configuration.setOkHttpClient(okHttpClient);

        OpenAiConfig dsConfig = new OpenAiConfig();
        configuration.setOpenAiConfig(dsConfig);
        dsConfig.setApiHost("https://api.moonshot.cn");
        dsConfig.setChatCompletionUrl("v1/chat/completions");
        dsConfig.setApiKey("sk-V9HGqFxTx0vW7yB0TwRoZ9wjarMlR7usHXQ7IMKw7pakqnJv");

        AiService aiService = new AiService(configuration);
        IChatService ds = aiService.getChatService(PlatformType.OPENAI);

        // 构建请求参数
        ChatCompletion chatCompletion = ChatCompletion.builder()
                .model("moonshot-v1-8k")
                .message(ChatMessage.withSystem(system))
                .message(ChatMessage.withUser(content))
                .build();

        // 发送对话请求
        return ds.chatCompletion(chatCompletion);
    }

}
